package com.yjd.comm.encrypt;

import com.alibaba.fastjson.JSON;
import com.yjd.comm.util.StringUtil;
import org.apache.commons.codec.binary.Base64;


import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjd-wap on 2018/1/18.
 */
public class Test {

    private static  String publicKey1 ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDkBeU9Icd+owsn6ROd6aC8InWQFpG8seuY5e7NEaKASxsXOXkCusRkYo0PktTYMIkyUDyqDpv69bmVBrRt2flifIrAQuXmi4eE8XNa/T4bccLFL0NiQJ9ghdu77ptpqFbzqvA2nIcbyd8r/wmbxt2fPXghSXSehbK/Bc5Zi+HvQIDAQAB";

    private static String privateKey1 ="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIOQF5T0hx36jCyfpE53poLwidZAWkbyx65jl7s0RooBLGxc5eQK6xGRijQ+S1NgwiTJQPKoOm/r1uZUGtG3Z+WJ8isBC5eaLh4Txc1r9PhtxwsUvQ2JAn2CF27vum2moVvOq8DachxvJ3yv/CZvG3Z89eCFJdJ6Fsr8FzlmL4e9AgMBAAECgYAsCqmKSM2OoJgaPBrx1aYdQU6zNXnDNHTaESbzGU3f1wK7B+JBk9IbYIBGevAWalazn9epI93kJ6waEN+lguM5ehu+X3JiCv7mgIcylzSwVQysRPEPC1rmc/4WkTwFmE45PAJ8HrGKq9aHe2ZsG5Yc9M4LPpGOta9F53kuh7LGQQJBAO1pr87d6DRHSAN3W144kE6N39iVMQkXzG/fMK1IaFf35SuCAGIXSSUqMILcgisA81yICd1X4epWphBf5vX1920CQQCN3OwQtyYKiUs/ZKlP3vmQXPb8qZIqRN6pf5FKVpbt1knQqaaxHYl34VLr7nyygqkt6AteG22rsoUb5S4Xew+RAkEA2DtMHtPJ6+OIWW/aTeSiuolJeY/Yi3mbf8D+KrUqJATqO0g6ZRzkR+83I9kGzXgeLwbQRUMHxlGJCpzuVNLjuQJAZ+BAquyw2j417zFSGL34l5C6+b9g27qaIBrc1EYbNgQXAMd422/5XOKMyV1TyZKLNqYWzM3gvDx34zkpSuA9QQJAKh/EMjOxyMLCubU24eebHsf2otE73gNOFgCi/NFV7oxiODy8e1CH1azktB0xBbLv97sWJs3AarWMu5+cgQnv8g==";

    private static String publicKey= "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3A59Bdo9J2dI5XzNj0cUsxhmV\n" +
            "PrcegTtva1IwwqaOhNdIqlM37zWJJbFxcc7AevgCYsQBD0XKPhTh4CXpzLferkOY\n" +
            "sH+sRiw8h4PrpSb588f5XobUJR5XqTu/XDav7+3TPLrfuH/1Uyu7JFBwbQ7JYWBA\n" +
            "Zq8mvhF5ZQHjTA4THwIDAQAB";

    private static String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALcDn0F2j0nZ0jlf\n" +
            "M2PRxSzGGZU+tx6BO29rUjDCpo6E10iqUzfvNYklsXFxzsB6+AJixAEPRco+FOHg\n" +
            "JenMt96uQ5iwf6xGLDyHg+ulJvnzx/lehtQlHlepO79cNq/v7dM8ut+4f/VTK7sk\n" +
            "UHBtDslhYEBmrya+EXllAeNMDhMfAgMBAAECgYBs81wQlasx1Nmgy/sE9rGCOSCr\n" +
            "x50uLNMcPnC8lHShLZYYLUXDa8WcMvNSzOS/k7UnfnKfCt+KSbEW0uDfMb9XcEFd\n" +
            "y9vxJb2oivISDUYnoa5HmK+VtN6Uyknc9w4V3g4Lwvxrleti7gEPBktvnQKfwOZB\n" +
            "jBcbuP8F99BkJd+E+QJBAOmYnXaslcS0FPaYFm+yp7A1WKjAz2Z2JH4RkrBbbkMT\n" +
            "2DY7mMB8eLGUXweVDEnDzvzpyYSOnlH12mEBWx41dVMCQQDIkRbKIpTWgIuCEy0U\n" +
            "RxYgz2LcX8RR+Bo6mY/sG2w4NPVDnEubCUZvw0ABtVk6xnSXgwOFnG+5TMs6hsTb\n" +
            "3IWFAkB7jPvj8953HqRj1Qcw5vZobZ7KCsjnsZ8ViufiHUnRcqKN9LGTRYJ0sjwc\n" +
            "/4lzTnyhKmoqkWBNi1KIsjXmYXH/AkEAvYvg7ccgwldZgOR+hdtonjlgRMR81LqU\n" +
            "jyzxR0HjVHs4D112Vl69ooHzy6mHWnSTW10Fxw/Axm484mLbSK1vwQJAIPaXi+xc\n" +
            "I9x8hwTm5m3WxYsEPuCzRiVk43JVcaHwQbOR/9KwxZYyfLW5mFbLWdWlRsxPeg02\n" +
            "kUZaTk8AnSvY0w==";
    public static void main(String[] args) {
        String s = EncryptTool.generateRandom32();
        String s1 = EncryptTool.generateRandom16();


        Map param = new HashMap();
        param.put("order_id","RR2222");
        param.put("agent_order_id", "DD356666");
        param.put("remit_result","1");
        param.put("remit_time","20171101235901");
        param.put("code","");
        param.put("msg","");

        String s2 = AesUtil.sortMapByKey(param);
        String bytes = AesUtil.encryptByECB(s2, s);

        param.put("sign",bytes);

      //  byte[] s3 = AesUtil.decryptByECB(bytes, s);
        String s3 = JSON.toJSONString(param);
        System.out.println("加密前，随机验证码32"+s);
        System.out.println("加密前，随机验证码16"+s1);
        System.out.println("接口数据加密前："+s2);
        System.out.println("接口请求数据base64之前数据："+s3);
        String s4 = Base64.encodeBase64String(s3.getBytes());
        System.out.println("接口请求数据base64之后数据："+s4);

        String s5 = RsaUtil.encryptByRsaPub(s,publicKey);
        System.out.println("公钥加密32位秘钥数据："+s5);

        Map request = new HashMap();
        request.put("data",s4);
        request.put("sc",s5);
        request.put("iv",s1);
        System.out.println("加密完成后请求接口数据："+JSON.toJSONString(request));

        String s6 = RsaUtil.decryptByRsaPri(s5,privateKey);
        System.out.println("私钥解密后的秘钥:"+s6);


        String s7 =  new String(Base64.decodeBase64(s4));
        System.out.println("base64 data数据明文："+s7);

        Map paseMap = StringUtil.getObject(s7,Map.class);

        String s8 = paseMap.get("sign").toString();
        System.out.println("请求签名数据："+s8);

        paseMap.remove("sign");

        String s9 = AesUtil.sortMapByKey(paseMap);
        System.out.println("解密后明文map排序："+s9);

        String s10 = AesUtil.encryptByECB(s9, s6);
        System.out.println("解密后生成签名："+s10);

        if(!bytes.equals(s10)){
            System.out.println("数据验签失败");
        }

    }
}
